#!/usr/bin/env python3
# _*_coding:utf-8 _*_
__author__ = "Gao"

import sys
import math
import pygame
import time

from pygame.locals import *

pygame.init()

screen = pygame.display.set_mode((600,500))
pygame.display.set_caption('推箱子')

pygame.key.set_repeat(200,500)

speed=0.1
player = {
    'position': [400,400,10,10],
    'width': 0,
    'color' : (255,255,255)
}

box_1 = {
    'position': [300,300,50,50],
    'width': 0,
    'color' : (255,255,0)
}

player_move = None    # 玩家移动方向

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()
    screen.fill((0, 0, 200))
    pygame.draw.rect(screen, player['color'], player['position'], player['width'])
    pygame.draw.rect(screen, box_1['color'], box_1['position'], box_1['width'])

    keys = pygame.key.get_pressed()

    # 移动人
    if player_move:
        if not keys[player_move]:
            K_MOVE = False
        else:
            K_MOVE = True
    else:
        K_MOVE = False
    if keys[K_LEFT]:
        if not K_MOVE:
            player_move = K_LEFT
            player['position'][0] -= speed
            K_MOVE = True
        elif player_move == K_LEFT:
            player['position'][0] -= speed
    if keys[K_RIGHT]:
        if not K_MOVE:
            player_move = K_RIGHT
            K_MOVE = True
            player['position'][0] += speed
        elif player_move == K_RIGHT:
            player['position'][0] += speed
    if keys[K_UP]:
        if not K_MOVE:
            player_move = K_UP
            K_MOVE = True
            player['position'][1] -= speed
        elif player_move == K_UP:
            player['position'][1] -= speed
    if keys[K_DOWN]:
        if not K_MOVE:
            player_move = K_DOWN
            K_MOVE = True
            player['position'][1] += speed
        elif player_move == K_DOWN:
            player['position'][1] += speed

    pygame.display.update()
